'use strict'

class db_userstep{

    constructor(mdb){
        this.db = mdb
    }

    async getreviewing(){
        let sql = `SELECT reviewing.id,reviewing.time,appname,funcname,icon,review,reason,users.username FROM reviewing,users WHERE reviewing.openid=users.id and review = 0;`
        let r = await this.db.query(sql)
        let data = r.rows.map((item)=>(
            {
                ...item,
                icon:`https://www.licnzh.cn:1234/static/usercreate/icon/${item.icon}` 
            }
            )
        )
        return data
    } 

    async getreviewedtrue(){
        let sql = `SELECT reviewing.id,appname,funcname,icon,review,reason,users.username,reviewing.time,reviewing.reason FROM reviewing,users WHERE reviewing.openid=users.id and review = 1;`
        let r = await this.db.query(sql)
        let data = r.rows.map((item)=>(
            {
                ...item,
                icon:`https://www.licnzh.cn:1234/static/usercreate/icon/${item.icon}` ,
            }
            )
        )
        return data
    } 

    async getreviewedfalse(){
        let sql = `SELECT reviewing.id,reviewing.time,appname,funcname,icon,review,reason,users.username FROM reviewing,users WHERE reviewing.openid=users.id and review = 2;`
        let r = await this.db.query(sql)
        let data = r.rows.map((item)=>(
            {
                ...item,
                icon:`https://www.licnzh.cn:1234/static/usercreate/icon/${item.icon}` 
            }
            )
        )
        return data
    } 

    async getStepDetail(funcid){
        let sql = `select * from usercreateappfunc  where funcid = ${funcid};`
        let r = await this.db.query(sql);
        let data = r.rows.map((item)=>(
            {
                ...item,
                img:`https://www.licnzh.cn:1234/static/usercreate/stepimage/${funcid}/${item.img}` 
            }
            )
        )
        return data;
    }

    //通过审核
    async getreviewed_true(id,reason){
        let sql0=`select openid from reviewing where id = ${id}`
        let r0=await this.db.query(sql0)
        let openid = r0.rows[0].openid

        let sql=`update reviewing set review=1,reason='${reason}' where id=${id}`
        let r = await this.db.query(sql)

        let sql1 = `SELECT integral FROM users WHERE id='${openid}'`
        let r1 = await this.db.query(sql1)
        let point1 = r1.rows[0].integral
        let point2 = point1 + 66
        let sql2 = `UPDATE users SET integral = ${point2} where id='${openid}'`
        await this.db.query(sql2)

        let data=r.rows
        return data
    }

    //审核驳回
    async getreviewed_false(id,reason){
        let sql=`update reviewing set review=2,reason='${reason}' where id=${id}`
        let r=await this.db.query(sql)
        let datas=r.rows
        return datas
    }

    reviewStatus(status){
        if(status == 0){
            return '审核中'
        }else if(status == 1){
            return '已发布'
        }else if(status == 2){
            return '未通过'
        }
    }

    //微信用户获得自己写过的步骤
    async getWxUserOwnStep(openid){
        let sql = `select id,appname,funcname,icon,review,reason,time from reviewing where openid = '${openid}';`
        let r = await this.db.query(sql);
        let data = r.rows.map(
            (item)=>(
                {
                    ...item,
                    icon:`https://www.licnzh.cn:1234/static/usercreate/icon/${item.icon}` ,
                    // review:this.reviewStatus(item.review)
                }
            )
        )
        return data;
    }
}

module.exports = db_userstep